#include<iostream>
#include<unistd.h>
#include<fcntl.h>
#include<string>
#include<cerrno>
#include<cstring>
using namespace std;
int main()
{
    //创建管道
    int pipes[2]={0};
    int n=pipe(pipes);
    if(n==-1)
    {
        cout<<"error"<<":"<<errno<<":"<<strerror(errno)<<endl;
        exit(1);
    }
    pid_t id=fork();
    if(id==0)
    {
        //子进程工作，写入
        cout<<"我是子进程:"<<getpid()<<endl;
        close(pipes[0]);
        char child_buf[1024]="Hello world! My name is shw and I am very cool!";
        ssize_t child_num=write(pipes[1],child_buf,sizeof(child_buf)-1);
        if(child_num<0)
        {
            cout<<"error"<<":"<<errno<<strerror(errno)<<endl;
            exit(1);
        }
        //子进程处理完就要退出
        close(pipes[1]);
        exit(0);
    }
    //父进程开始工作，读取
    close(pipes[1]);
    char parent_buf[1024]={0};
    ssize_t parent_num=read(pipes[0],parent_buf,1024);
    cout<<"我是父进程："<<parent_buf<<endl;
    close(pipes[0]);
    return 0;
} 